var _countDowncontainer=0;
var _currentSeconds=0;
var _onZeroFunction;
var _warningValue;
var _warningCssClass;
var _enabled = true;

function InitCountDown(strContainerID, initialValue, warningValue, warningCssClass, onZeroFn) 
{
    _onZeroFunction = onZeroFn;
    _countDowncontainer = document.getElementById(strContainerID);
    _warningValue = warningValue;
    _warningCssClass = warningCssClass;
    if (!_countDowncontainer) 
    {
      alert("count down error: container does not exist: "+strContainerID+
          "\nmake sure html element with this ID exists");
      return;
    }
    SetCountdownText(initialValue);
}

function CountDownTick() 
{
    if(!_enabled)
    {
        return;
    }
    if (_currentSeconds == _warningValue) 
    {
      _countDowncontainer.className = _warningCssClass;
    }
    if(_currentSeconds <= 0) 
    {
        _onZeroFunction();
        return;
    }
    SetCountdownText(_currentSeconds-1);
    window.setTimeout("CountDownTick()", 1000);
}

function ReactivateCountdown(initialValue, warningValue, cssClass)
{
    _countDowncontainer.className = cssClass;
    _warningValue = warningValue;
    SetCountdownText(initialValue);
    window.setTimeout("CountDownTick()", 1000);
}

function SetCountdownText(seconds) 
{
    _currentSeconds = seconds;
    //get minutes:
    var minutes=parseInt(seconds/60);
    //shrink:
    seconds = (seconds%60);
    //get hours:
    var hours=parseInt(minutes/60);
    //shrink:
    minutes = (minutes%60);
    //build text:
    var strText = AddZero(hours) + ":" + AddZero(minutes) + ":" + AddZero(seconds);
    //apply:
    _countDowncontainer.innerHTML = strText;
}

function AddZero(num) 
{
    return ((num >= 0)&&(num < 10))?"0"+num:num+"";
} 

function SetEnabled(value)
{
    _enabled = value;
}